﻿Ext.define('Oa.AuditionGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'Widget.AuditionGrid',

    // override
    initComponent: function () {
        var store = Ext.create('Ext.data.TreeStore', {
            model: 'TreeModel',
            proxy: {
                type: 'ajax',
                url: '/dept.mvc/DeptTreeByEmp',
                extraParams: {
                    controllerName: 'Audition'
                },
                reader: {
                    type: 'json'
                }
            },
            // 设置根节点
            root: {
                expanded: true
            },
            autoLoad: true
        });

        this.columns = [
            {
                header: "应聘人", width: 100, dataIndex: 'Name'
            }, {
                header: "应聘部门", width: 140,
                dataIndex: 'DeptName'
            }, {
                header: "应聘职位", width: 140,
                dataIndex: 'Position'
            }, {
                header: "联系电话", width: 100,
                dataIndex: 'Phone'
            }, {
                header: "面试官", width: 100,
                dataIndex: 'Interviewer'
            }, {
                header: "面试时间", width: 140,
                dataIndex: 'InterviewTime'
            }, {
                header: "面试结果", flex: 1,
                dataIndex: 'Result'
            }, { header: "操作", dataIndex: 'Id', width: 140, renderer: function (value, a, reader, c, d, ds) {
                var links = [];
                links.push('<a href="#" onclick="var url =\'AuditionInfo.aspx?id=' + value + '\';window.showModalDialog(url, window, \'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;\');">查看</a>');
                links.push('<a href="#" onclick="var url =\'AuditionEdit.aspx?id=' + value + '\';window.showModalDialog(url, window, \'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;\');Ext.getCmp(\'AuditionMgr\').getStore().reload();">编辑</a>');
                links.push('<a href="#" onclick="return delAudition(\'' + value + '\');">删除</a>');
                return links.join("&nbsp;");
            }
            }
		];
        // Set up a model to use in our Store
        Ext.define('AuditionModel', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'Id'
            }, {
                name: 'Name'
            }, {
                name: 'DeptId'
            }, {
                name: 'DeptName'
            }, {
                name: 'Position'
            }, {
                name: 'Phone'
            }, {
                name: 'Interviewer'
            }, {
                name: 'Reminder'
            }, {
                name: 'InterviewTime'
            }, {
                name: 'Result'
            }]
        });

        var ds = Ext.create('Ext.data.Store', {
            model: 'AuditionModel',
            pageSize: 20, // items per page
            proxy: {
                type: 'ajax',
                url: '/Audition.mvc/Find',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'//分页使用
                }
            },
            autoLoad: false
        });
        ds.load({ params: { start: 0, limit: 20} });
        ds.on('beforeload', function (thiz, options) {
            Ext.apply(thiz.proxy.extraParams, { deptId: Ext.getCmp('treeDeptAudition').getValue(), name: Ext.getCmp('txtNameAudition').getValue() });
        });
        this.store = ds;

        this.dockedItems = [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                xtype: 'tbtext',
                text: '应聘部门:'
            }, {
                xtype: 'treepicker',
                id: 'treeDeptAudition',
                displayField: 'text',
                forceSelection: true,
                editable: false,
                width: 250,
                store: store
            }, {
                xtype: 'tbtext',
                text: '应聘人:'
            }, {
                xtype: 'textfield',
                id: 'txtNameAudition',
                emptyText: '人名的一或几个字',
                blankText: '人名的一或几个字',
                anchor: '100%'
            }, {
                text: "查询",
                iconCls: 'icon-search',
                handler: function () {
                    Ext.getCmp('pagingAudition').moveFirst();
                }
            }, '-', {
                text: "添加",
                tooltip: "添加新的信息",
                iconCls: "addicon",
                handler: function () {
                    var url = 'AuditionAdd.aspx';
                    var retVal = window.showModalDialog(url, window, 'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;');
                    if (retVal != undefined && !isNaN(retVal)) {
                        url = 'AuditionEdit.aspx?id=' + retVal;
                        window.showModalDialog(url, window, 'dialogWidth=850px;dialogHeight=550px;status=no;help=no;');
                        ds.reload();
                    }
                }
            }]
        }, {
            xtype: 'pagingtoolbar',
            id: 'pagingAudition',
            store: ds,
            dock: 'bottom',
            displayInfo: true
        }];

        // finally call the superclasses implementation
        Oa.AuditionGrid.superclass.initComponent.call(this);
    }
});

function delAudition(id) {
    Ext.Msg.show({
        title: '系统确认',
        msg: '你是否确定删除该行数据?',
        buttons: Ext.Msg.YESNO,
        scope: this,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '/Audition.mvc/Delete',
                    success: function (response, options) {
                        var responseMessage = Ext.JSON.decode(response.responseText);
                        if (responseMessage.Result) {
                            Ext.Msg.alert('提示', '已经成功删除数据。');
                            if (Ext.getCmp("AuditionMgr") != null) { Ext.getCmp("AuditionMgr").getStore().reload(); }
                        }
                        else {
                            Ext.Msg.alert('提示', '删除失败，请您重试。');
                        }
                    },
                    failure: function (action) {
                        if (action.failureType == 'server') {
                            obj = Ext.JSON.decode(action.response.responseText);
                            Ext.Msg.alert('错误:', obj.errors.reason);
                        }
                    },
                    params: {
                        id: id
                    }
                });
            }
        }
    });
    return false;
}